"use client";

import { Button } from "@/components/ui/button";
import { useSession } from "next-auth/react";
import { signOut } from "next-auth/react";
import { useRouter } from "next/navigation";
import { SheetBox } from "./profile-button";

export const AuthButton = () => {
  const { data: session } = useSession();
  const router = useRouter();
  const user = session?.user;

  const handleSignOut = async () => {
    await signOut({
      redirect: false, // 禁用默认重定向
    });
    window.location.href = "/";
  };


  return (
    <div className="flex items-center">
      <SheetBox>
      </SheetBox>
      <Button variant="secondary" onClick={user ? handleSignOut : () => router.push("/sign-in")}>
        {user ? "退出登录" : "登录"}
      </Button>
    </div>
  );
};